package com.elong.android.specialhouse.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.adapter.ApartmentAreaLeftAdapter;
import com.elong.android.specialhouse.adapter.ApartmentAreaMidAdapter;
import com.elong.android.specialhouse.adapter.ApartmentAreaRightAdapter;
import com.elong.android.specialhouse.entity.ApartmentSearchChildDataInfo;
import com.elong.android.specialhouse.entity.ApartmentSearchDataInfo;
import com.elong.android.specialhouse.entity.ApartmentSearchDataInterface;
import com.elong.android.specialhouse.entity.ApartmentSelectSerchChildInfo;
import com.elong.infrastructure.entity.Group;

import java.util.ArrayList;

public class ApartmentAreaView extends LinearLayout{

	private Context context;

	// 是否显示"不限"
	private boolean isShowNoLimit = false;
	private View headerMid;
	private View headerRight;

	private ListView listviewLeft;
	private ListView listviewMid;
	private ListView listviewRight;

	private ApartmentAreaLeftAdapter adapterLeft;
	private ApartmentAreaMidAdapter adapterMid;
	private ApartmentAreaRightAdapter adapterRight;

	private ArrayList<ApartmentSearchChildDataInfo> adapterDataLeft;
	private ArrayList<ApartmentSearchChildDataInfo> adapterDataMid;
	private ArrayList<ApartmentSearchChildDataInfo> adapterDataRight;

	private OnApartmentAreaViewListener mListener;

	public ApartmentAreaView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.apartmentarea_view, this);
		this.context = context;
		initView();
		addListener();
	}

	private void initView() {
		headerMid = LayoutInflater.from(context).inflate(R.layout.apartmentareamid_header, null);
		headerRight = LayoutInflater.from(context).inflate(R.layout.apartmentarearight_header, null);

		listviewLeft = (ListView) findViewById(R.id.listview_left);
		listviewMid = (ListView) findViewById(R.id.listview_mid);
		listviewRight = (ListView) findViewById(R.id.listview_right);

		adapterDataLeft = new ArrayList<ApartmentSearchChildDataInfo>();
		adapterDataMid = new ArrayList<ApartmentSearchChildDataInfo>();
		adapterDataRight = new ArrayList<ApartmentSearchChildDataInfo>();

		adapterLeft = new ApartmentAreaLeftAdapter(context, adapterDataLeft);
		adapterMid = new ApartmentAreaMidAdapter(context, adapterDataMid);
		adapterRight = new ApartmentAreaRightAdapter(context, adapterDataRight);
	}

	/**
	 * 设置监听
	 * 
	 * @param listener
	 */
	public void setOnSelectLeafListener(OnApartmentAreaViewListener listener) {
		this.mListener = listener;
	}

	/**
	 * 初始化view数据,以及是否显示"不限",只是初始化,不要反复调用~
	 * 
	 * @param data
	 */
	public void initData(ApartmentSearchDataInfo data, ApartmentSelectSerchChildInfo selectInfo, boolean isShowNoLimit) {
		this.isShowNoLimit = isShowNoLimit;
		if (isShowNoLimit) {
			listviewMid.addHeaderView(headerMid);
			listviewRight.addHeaderView(headerRight);
		}

		// setAdapter之前设置header,否则部分rom报错╮(╯▽╰)╭
		listviewLeft.setAdapter(adapterLeft);
		listviewMid.setAdapter(adapterMid);
		listviewRight.setAdapter(adapterRight);

		if (data.getChildDataInfos() == null || data.getChildDataInfos().size() <= 0) {
			if (mListener != null) {
				mListener.exeNoAreaData();
			}
			return;
		}

		// 处理默认选中信息
		if (selectInfo != null && selectInfo.getChildDataInfos() != null && 0 < selectInfo.getChildDataInfos().size()
				&& selectInfo.getChildDataInfos().get(0) != null) {
			Log.d("mSSInfo", selectInfo.getChildDataInfos().get(0).getName());
			Log.d("mSSInfo", selectInfo.getChildDataInfos().get(0).getName());
			selectData(data.getChildDataInfos(), selectInfo.getChildDataInfos().get(0));
		} else {
			// 无ApartmentSelectSerchChildInfo信息,默认选中第0个
			data.getChildDataInfos().get(0).setSelect(true);
		}

		adapterLeft.setData(data.getChildDataInfos(), true);

		int p1 = getSelectPosition(data.getChildDataInfos());
		Group<ApartmentSearchChildDataInfo> data1 = data.getChildDataInfos().get(p1).getChildDataInfos();
		if (data1 == null || data1.size() <= 0) {
			return;
		}
		boolean isSubway = ("地铁").equals(data.getChildDataInfos().get(p1).getName());
		adapterMid.setBottomLineVisibility(!isSubway);
		if (isShowNoLimit) {
			if (isSubway && 0 < listviewMid.getHeaderViewsCount()) {
				listviewMid.removeHeaderView(headerMid);
			} else if (listviewMid.getHeaderViewsCount() <= 0) {
				try {
					listviewMid.addHeaderView(headerMid);
				} catch (IllegalStateException e) {
				}
			}
		}
		adapterMid.setData(data1, true);

		int p2 = getSelectPosition(data.getChildDataInfos().get(p1).getChildDataInfos());
		Group<ApartmentSearchChildDataInfo> data2 = data.getChildDataInfos().get(p1).getChildDataInfos().get(p2)
				.getChildDataInfos();
		if (data2 == null || data2.size() <= 0) {
			return;
		}
		adapterRight.setData(data2, true);
		listviewRight.setVisibility(View.VISIBLE);
	}

	private void addListener() {
		listviewLeft.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int posion, long id) {
				if (!adapterDataLeft.get(posion).isSelect()) {
					getSelectedPostion(adapterDataLeft, posion);
					ApartmentSearchChildDataInfo data = adapterDataLeft.get(posion);
					if (data.getName().equals("地铁")) {
						adapterMid.setBottomLineVisibility(false);
						if (isShowNoLimit && 0 < listviewMid.getHeaderViewsCount()) {
							listviewMid.removeHeaderView(headerMid);
						}
						adapterMid.setData(data.getChildDataInfos(), false);
						if (0 < data.getChildDataInfos().size()) {
							listviewRight.setVisibility(View.VISIBLE);
							int p = getSelectPosition(data.getChildDataInfos());
							adapterRight.setData(data.getChildDataInfos().get(p).getChildDataInfos(), false);
						} else {
							listviewRight.setVisibility(View.GONE);
						}
					} else {
						listviewRight.setVisibility(View.GONE);
						adapterMid.setBottomLineVisibility(true);
						if (isShowNoLimit && listviewMid.getHeaderViewsCount() <= 0) {
							try {
								listviewMid.addHeaderView(headerMid);
							} catch (IllegalStateException e) {
							}
						}
						adapterMid.setData(data.getChildDataInfos(), false);
					}
					adapterLeft.notifyDataSetChanged();
				}
			}
		});

		listviewMid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int posion, long id) {
				if (id == -1) {
					// 点击了header或者footer,listvieMid只有一个header,即headerMid
					mListener.exeSelectLeaf(null);
					return;
				}
				posion = posion - listviewMid.getHeaderViewsCount();
				// 无脑判断posion异常,正常不会<0
				if (posion < 0) {
					posion = 0;
				}
				ApartmentSearchChildDataInfo data = adapterDataMid.get(posion);
				if (data.isLeaf()) {
					if (mListener != null) {
						mListener.exeSelectLeaf(data);
					}
				} else {
					if (0 < data.getChildDataInfos().size()) {
						getSelectedPostion(adapterDataMid, posion);
						adapterMid.notifyDataSetChanged();
						listviewRight.setVisibility(View.VISIBLE);
						adapterRight.setData(data.getChildDataInfos(), false);
					} else {
						listviewRight.setVisibility(View.GONE);
						adapterRight.setData(data.getChildDataInfos(), false);
					}
				}
			}
		});

		listviewRight.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int posion, long id) {
				if (id == -1) {
					// 点击了header或者footer,listvieMid只有一个header,即headerMid
					mListener.exeSelectLeaf(null);
					return;
				}
				posion = posion - listviewRight.getHeaderViewsCount();
				// 无脑判断posion异常,正常不会<0
				if (posion < 0) {
					posion = 0;
				}
				ApartmentSearchChildDataInfo data = adapterDataRight.get(posion);
				if (mListener != null) {
					mListener.exeSelectLeaf(data);
				}
			}
		});
	}

	/**
	 * 获取data中选中项的位置索引Postion
	 * 
	 * @param data
	 * @param pos
	 */
	private void getSelectedPostion(ArrayList<ApartmentSearchChildDataInfo> data, int pos) {
		for (int i = 0; i < data.size(); i++) {
			data.get(i).setSelect(pos == i);
		}
	}

	private void selectData(Group<ApartmentSearchChildDataInfo> childDataInfos,
			ApartmentSearchChildDataInfo ApartmentSearchChildDataInfo) {
		for (com.elong.android.specialhouse.entity.ApartmentSearchChildDataInfo info : childDataInfos) {
			ApartmentSearchDataInterface selectParentInfo = ApartmentSearchChildDataInfo.getParentInfo();
			ApartmentSearchDataInterface parentInfo = info.getParentInfo();
			boolean isparentNameSame = false;
			if (parentInfo != null) {
				String name = parentInfo.getName();
				String selectName = selectParentInfo.getName();
				if (name.equals(selectName)) {
					isparentNameSame = true;
				} else {
					isparentNameSame = false;
				}
			}
			if (info.getName().equals(ApartmentSearchChildDataInfo.getName()) && isparentNameSame) {

				info.setSelect(true);
				disposeChange(info, info.getName());
				break;
			} else {
				if (!info.isLeaf()) {
					selectData(info.getChildDataInfos(), ApartmentSearchChildDataInfo);
				}
			}
		}
	}

	private void disposeChange(ApartmentSearchChildDataInfo dataInfo, String name) {
		ApartmentSearchDataInterface parentInfo = dataInfo.getParentInfo();
		if (parentInfo instanceof ApartmentSearchChildDataInfo) {
			((ApartmentSearchChildDataInfo) parentInfo).setSelect(true);
			((ApartmentSearchChildDataInfo) parentInfo).setSelectChildName(name);
			disposeChange((ApartmentSearchChildDataInfo) parentInfo, name);
		}
	}

	/**
	 * 返回Group<ApartmentSearchChildDataInfo>中选中的项目索引
	 * 
	 * @param data
	 * @return
	 */
	private int getSelectPosition(Group<ApartmentSearchChildDataInfo> data) {
		if (data == null) {
			return 0;
		}
		for (int i = 0; i < data.size(); i++) {
			if (data.get(i).isSelect()) {
				return i;
			}
		}
		return 0;
	}

	/**
	 * @author WHB 酒店筛选区域视图监听
	 */
	public interface OnApartmentAreaViewListener {

		/**
		 * 无任何位置信息时的回调
		 */
		public void exeNoAreaData();

		/**
		 * 点击了叶子节点的回调
		 * 
		 * @param data
		 */
		public void exeSelectLeaf(ApartmentSearchChildDataInfo data);
	}

}
